import { defineStore } from "pinia";
import {displayByName} from "../cesiumTools/course"
export const useLineData = defineStore("lineData", {
  //初始状态,lineData: 初始化为空数组
  state: () => {
    return {
      lineData: [],
      disable:false,
    };
  },
  //访问,查询
  getters: {
    // 返回当前的 Viewer 对象
    Viewer() {
      return this.viewer;
    },
    // 返回当前的 Tileset 对象
    Tile() {
      return this.tileset;
    },
    // 返回一个经过处理的线路数据数组，包含每个线路的 name、id 和 name 属性。
    lineDisplay() {
      return this.lineData.map((item) => ({
        name: item.checked,
        id: item.id,
        name: item.name,
      }));
    },
    // 返回当前存储的所有线路数据
    allData() {
      return this.lineData;
    },
    isDisable(){
      return this.disable;
    }
  },
  //添加(设置)数据,
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.lineData = data.length ? data : [];
        resolve(data);
      });
    },
    setViewer(viewer) {
      return new Promise((resolve, reject) => {
        this.viewer = viewer;
        resolve(viewer);
      });
    },
    setTileset(tileset) {
      return new Promise((resolve, reject) => {
        this.tileset = tileset;
        resolve(tileset);
      });
    },
    watchLineData(type) {
      return new Promise((resolve) => {
        // 监听设定的类型
        if (type === "setViewer") {
          // 模拟监听 Viewer
          const viewer = this.viewer; // 获取当前 viewer
          resolve(viewer);
        } else if (type === "setData") {
          // 返回当前的 lineData
          const data = this.lineData;
          resolve(data);
        }
      });
    },
    displayLine(lineNames, isShow) {
      if (Array.isArray(lineNames) && this.lineData.length && !this.disable) {
        const stationNames = [];
        // 控制state数据
        this.lineData.forEach((item) => {
          const { stationsList, name } = item;
          if (lineNames.includes(name)) {
            item.checked = isShow;
            stationsList.forEach((station) => {
              stationNames.push(station.name);
            });
          }
        });
        // 控制地图元素
        displayByName(lineNames, stationNames, isShow);
      }
    },
	// 设置是否开启全局管理
    disableController(val) {
      this.disable = val;
    }
  },
});

export const useMeasureData = defineStore("measureData", {
  state: () => {
    return {
      measureData: [],
    };
  },
  getters: {
    // 获取全部信息
    allData() {
      return this.measureData;
    },
  },
  actions: {
    setData(data) {
      return new Promise((resolve, reject) => {
        this.measureData = data.length ? data : [];
        resolve(data);
      });
    },
  },
});

// 由于lineData的state是异步的，所以需要监听action来获取数据
export const watchLineData=(actionName='setData')=>{
  return new Promise((resolve,reject)=>{
      useLineData().$onAction(({name,store,args,after,onError})=>{
          if(name===actionName){
              after((res)=>{
                  console.log(res);
                  if(res){
                      resolve(res)
                  }
              })
              onError((e)=>{
                  reject(e)
              })
          }
      })
  })
}

export const controlAll = (isTure, subLineData) => {
  // 全选，控制所有站点，站线

  const checked = isTure
  const lineNames = subLineData.value.map((item) => item.name)
  const stationNames = []
  subLineData.value.forEach((item) => {
      const { stationsList } = item
      stationsList.forEach((station) => {
          stationNames.push(station.name)
      })
  })
  // 控制地图元素显示隐藏
  displayByName(lineNames, stationNames, checked)
  // 控制ui面板显示隐藏
  subLineData.value = subLineData.value.map((item) => {
      item.checked = checked
      return item
  })
}